package com.ruoyi.web.controller.equip;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.SelectEntity;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.equip.domain.EquipProduct;
import com.ruoyi.equip.domain.dto.ThingModelDto;
import com.ruoyi.equip.domain.entity.table.ExportParam;
import com.ruoyi.equip.service.IEquipProductService;
import com.ruoyi.equip.service.IThingModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/equip/product")
@Api(tags = "设备产品相关接口")
public class EquipProductController extends BaseController {

    @Autowired
    private IEquipProductService productService;

    @Autowired
    private IThingModelService thingModelService;


    @ApiOperation(("上传设备产品图片"))
    @PostMapping("/img")
    public AjaxResult img(@RequestParam("imgFile")MultipartFile file) throws Exception{
        if (!file.isEmpty()){
            String product = FileUploadUtils.upload(RuoYiConfig.getProductImgPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", product);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }



    @ApiOperation("查询所有设备产品")
    @GetMapping("/list")
    public TableDataInfo list(EquipProduct equipProduct) {
        startPage();
        List<EquipProduct> equipCategories = productService.selectAll(equipProduct);
        return getDataTable(equipCategories);
    }



    @ApiOperation("查询所有设备产品下拉框")
    @GetMapping("/selectList")
    public AjaxResult selectList() {
        List<SelectEntity> selectList = productService.selectList();
        return success(selectList);
    }


    /**
     * 新增设备产品
     */
    @ApiOperation("新增设备产品")
    @Log(title = "新增设备产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody EquipProduct equipProduct) {
        return toAjax(productService.addProduct(equipProduct));
    }

    /**
     * 修改设备产品
     */
    @ApiOperation("修改设备产品")
    @Log(title = "修改指定设备产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody EquipProduct equipProduct){
        return toAjax(productService.upateProduct(equipProduct));
    }



    /**
     * 删除设备产品
     */
    @ApiOperation("删除设备产品")
    @Log(title = "删除指定设备产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String id) {
        return toAjax(productService.deleteId(id));
    }




    /**
     * 查看物模型
     */
    @ApiOperation("查看物模型")
    @GetMapping("/getThingModelByProductKey/{productKey}")
    public AjaxResult getThingModelByProductKey(@PathVariable String productKey){
        return success(thingModelService.findByProductKey(productKey));
    }



    @ApiOperation("保存物模型")
    @Log(title = "新增产品物模型", businessType = BusinessType.INSERT)
    @PostMapping("/thingModel/save")
    public boolean saveThingModel(@Validated @RequestBody ThingModelDto thingModelDto) {
        return thingModelService.saveThingModel(thingModelDto);
    }

    @Log(title = "产品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,  ExportParam exportParam) {
    productService.export(response,exportParam.getProductIds());
    }

}
